package com.haier.npt.filter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.http.HttpStatus;

import com.haier.npt.authention.authc.RequestContextInfo;
import com.haier.npt.common.JsonResult;
import com.haier.npt.exception.BizBaseException;
import com.haier.npt.utils.JsonUtils;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.ZuulFilterResult;
import com.netflix.zuul.context.RequestContext;

/**
 * @Description:
 * @author: lixu
 * @date:   2018年7月12日 上午11:04:36   
 */
public abstract class AbstractProcessingFilter  extends ZuulFilter{

	@Autowired
	protected RouteLocator routeLocator;
	
	@Autowired
	private RequestContextInfo requestContextInfo;
	
	public RequestContext getCtx(){
		return requestContextInfo.getCtx();
	}
	
	public String getToken(){
		return requestContextInfo.getToken();
	}
	
	public void setToken(String token){
		requestContextInfo.setToken(token);
	}
	
	public Long getUserid() {
		return requestContextInfo.getUserid();
	}

	public void setUserid(Long userid) {
		requestContextInfo.setUserid(userid);
	}
	
	@Override
	public ZuulFilterResult runFilter(){
		ZuulFilterResult filterResult = super.runFilter();
		if(filterResult.getException() != null){
			filterResult.getException().printStackTrace();
		}
		return filterResult;
	}
	
	/**
	 * @Description: 认证失败的方法
	 * @param: @param ctx      
	 * @return: void      
	 * @throws   
	 */
	protected void onLoginFailure(RequestContext ctx){
		//设置一个变量，标识未通过登录
		ctx.set("loginFaulure",true);
		
		ctx.getResponse().setContentType("application/json;charset=utf-8");
		ctx.setSendZuulResponse(false);
		ctx.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
		ctx.setResponseBody(JsonUtils.toJSONString(JsonResult.error(401, "未授权错误")));
	}
	
	/**
	 * @Description: 权限检查失败的方法
	 * @param: @param ctx      
	 * @return: void      
	 * @throws   
	 */
	protected void onAuthFailure(RequestContext ctx){
		ctx.getResponse().setContentType("application/json;charset=utf-8");
		ctx.setSendZuulResponse(false);
		ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
		ctx.setResponseBody(JsonUtils.toJSONString(JsonResult.error(403, "无访问权限")));
	}
	
	/**
	 * @Description: 处理zuul的异常，默认zuul发生异常 请求会无任何反应并且控制台也没用反应
	 * @param: @param ex      
	 * @return: void      
	 * @throws   
	 */
	protected void processZuulException(Throwable ex){
		requestContextInfo.getCtx().set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		requestContextInfo.getCtx().set("error.i18n", ex);
	}
	
	protected void processBizZuulException(BizBaseException ex){
		requestContextInfo.getCtx().getResponse().setContentType("application/json;charset=utf-8");
		requestContextInfo.getCtx().setSendZuulResponse(false);
		requestContextInfo.getCtx().setResponseStatusCode(HttpStatus.OK.value());
		requestContextInfo.getCtx().setResponseBody(JsonUtils.toJSONString(JsonResult.error(ex.getCode(), ex.getMessage())));
	}
	
	/**
	 * 截取关键字紧跟着的编号
	 * @param url
	 * @param keyWord
	 * @return
	 */
	protected  String handleKeyWords(String url , String keyWord){
		String  substr= StringUtils.substringAfter(url, keyWord);
		String content = StringUtils.substringBefore(substr, "/");
		if(content.matches("\\d+")){
			return content ; 
		}
		return null; 
	}

	

}
